<template>
  <div class="mod-config">
    <basic-container>
      <el-row>
        <el-col :span="24">
          <el-button v-if="permissions.mp_wxdeptlink_add" icon="el-icon-plus" type="primary" size="mini" @click="addOrUpdateHandle()">新增</el-button>
          <!-- <el-button icon="el-icon-search" circle size="mini" @click="handleSearchShow" style="float:right;"></el-button>
          <el-button icon="el-icon-refresh" circle size="mini" @click="refreshChange" style="float:right;"></el-button> -->
          <el-button type="primary" style="float:right;margin-left: 15px;" @click="configuration">统一配置</el-button>
          <el-select style="float:right;" v-model="wechatId">
            <el-option value="1" label="金华眼科医院"></el-option>
          </el-select>
          <!-- <el-button icon="el-icon-menu" circle size="mini" style="float:right;"></el-button>-->
        </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="deptName" label="门店"></el-table-column>
          <el-table-column prop="status" label="状态(1-启用,0-停用)"></el-table-column>
          <el-table-column prop="wechatId" label="微信公众号"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="permissions.mp_wxdeptlink_edit" type="text" size="small" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button v-if="permissions.mp_wxdeptlink_del" type="text" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    </basic-container>
  </div>
</template>

<script>
  import { fetchWxDeptLinkList, delWxDeptLinkObj, editAllWxDeptLink } from '@/api/mp/wxdeptlink'
  import TableForm from './wxdeptlink-form'
  import {
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        wechatId: '',
        dataForm: {
          key: ''
        },
        dataList: [
          {deptId: '1',deptName: '视光中心',status: '1'},
          {deptId: '2',deptName: '江南防控',status: '0'},
        ],
        deptIdList: [],
        multipleSelection: [],
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created() {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchWxDeptLinkList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {

          // this.dataList = response.data.data.records
          // this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delWxDeptLinkObj(id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
      // 多选表选中事件
      handleSelectionChange(val){
        this.multipleSelection = val
        let deptIdList = []
        val.forEach(item => {
          deptIdList.push(item.deptId)
        })
        this.deptIdList = [...deptIdList]
      },
      // 统一配置
      configuration(){
        console.log(this.wechatId)
        console.log(this.deptIdList)
        let params = {
          wechatId: this.wechatId,
          ids: this.deptIdList
        }
        editAllWxDeptLink(params).then(response => {
          console.log(response)
        })
      }
    }
  }
</script>
